package com.lgj.springboot_lgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Comment;
import com.lgj.springboot_lgj.dao.basic.CommentMapper;
import com.lgj.springboot_lgj.dao.ext.CommentMapperExt;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper mapper;
    @Autowired
    private CommentMapperExt mapperExt;
    private List<Integer> sonIds = new ArrayList();
    private List<Integer> allIds = new ArrayList();

    //获取子评论id
    @Override
    public List<Integer> obtainCommentSonId(Integer id){
        List<Integer> list = mapperExt.selectCommentParentId(id);
        /*System.out.println(list);*/
        if(!list.isEmpty()){
            for (Integer it : list) {
                sonIds.add(0, it);
                this.obtainCommentSonId(it);
            }
        }
        return sonIds;
    }

    //根据id删除评论
    @Override
    public void deleteById(Integer id) {
            //获取子id
            allIds = this.obtainCommentSonId(id);
            //把父id放入集合
            allIds.add(id);
        for (Integer a : allIds) {
            //单独获取每个id
            //设置文章id为null
            mapperExt.updateArticle(a);
            //删除id对应的评论
            mapperExt.deleteAllComment(a);
        }
            allIds.clear();

    }
    //批量删除评论
    @Override
    public String batchDelete(int[] comment_id) {
        for (int i : comment_id) {
            //获取子id
            allIds = this.obtainCommentSonId(i);
            //把父id放入集合
            allIds.add(i);
            for (Integer c : allIds) {
                //单独获取每个id
                //设置文章id为null
                mapperExt.updateArticle(c);
                //删除id对应的评论
                mapperExt.deleteAllComment(c);
            }
            allIds.clear();

        }
        return "删除成功";
    }

    //审核评论
    @Override
    public void checkComment(int id, int status){
        mapperExt.updateByStatus(id,status);
    }

    //分页查询所有评论信息
    @Override
    public PageInfo<Comment> pageQuery(int pageNum, int pageSize, String keyWords) {
        if(keyWords == null){
            keyWords = "";
        }
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize,true);
        //执行分页查询
        List<Comment> list = mapperExt.findPartComment(pageNum,pageSize,keyWords);
        PageInfo<Comment> pageInfo = new PageInfo<>(list);
        //返回数据
        return pageInfo;
    }
}
